/**
 * Advanced Software Engineering(Monica Farrow)
 * CustomerGUI class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depotViews;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import depotModels.CustomerQueue;
import depot.Customer;
import depotInterfaces.DepotObserver;

/**
 * CustomerGUI class - displays data from CustomerQueue subject
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class CustomerGUI extends JFrame implements DepotObserver {

	private static final long serialVersionUID = 1L;

	private CustomerQueue model;    // holds customers awaiting processing

	// Title panel
	JPanel titlePanel = new JPanel();
	JLabel guiTitle = new JLabel();  // GUI heading

	// GUI componenets
	JPanel custsPanel = new JPanel();
	Object[][] custsRows = null;                    // holds details of processed customers
	JLabel totalCusts = new JLabel();               // holds total no. processed customers
	JTable custsTable = new JTable();               // table of processed customers
	DefaultTableModel custsTabModel = new DefaultTableModel(); // table model for custsTable

	// holds column headers 
	String[] columns = {"Customer ID", "Customer name", "Parcel ID"};

	/**
	 * constructor for CustomerGUI
	 * registers CustomerGUI with CustomerQueue object (model)
	 * contained in the parameter
	 * @param model  the CustomerQueue object
	 */
	public CustomerGUI(CustomerQueue model) {
		this.model = model;

		// register with the model
		model.registerObserver(this);

		// create and display CustomerGUI 
		createDisplay();

	}

	/**
	 * creates and starts the GUI
	 */
	public void createDisplay() {
		// set GUI titlebar
		this.setTitle("Customer Queue");

		// Sets location of GUI
		this.setLocation(680,150);
		
		// disable GUI close button
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

		// Set size of GUI
		this.setMinimumSize(new Dimension(550, 145));

		// set layout
		this.setLayout(new BorderLayout());

		// create & add title panel
		createTitlePanel();
		this.add(titlePanel, BorderLayout.NORTH);

		// create & add current customer panel
		createCustsQueuePanel();
		this.add(custsPanel, BorderLayout.CENTER);

		// Pack and make GUI visible
		this.pack();
		this.setVisible(true);
	}


	/**
	 *  Creates the customers queue panel
	 */
	public void createCustsQueuePanel() {

		custsPanel.setLayout(new BorderLayout());
		custsPanel.setSize(550, 145);

		// create table and make un-editiable
		custsTable = new JTable(new DefaultTableModel(custsRows, columns))
		{               
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rows, int columns)
			{
				return false;
			}
		};

		// set table model (allows insert/delete rows)
		custsTabModel = (DefaultTableModel) custsTable.getModel();

		// Align all the cell to center view
		DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
		cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
		custsTable.getColumn("Customer ID").setCellRenderer( cellRenderer );
		custsTable.getColumn("Customer name").setCellRenderer( cellRenderer );
		custsTable.getColumn("Parcel ID").setCellRenderer( cellRenderer );

		// Size of scrollable area
		custsTable.setPreferredScrollableViewportSize(new Dimension(500,100));
		custsTable.setFillsViewportHeight(false);

		JScrollPane custsContainer = new JScrollPane(custsTable);


		// add components to panel
		custsPanel.add(custsContainer, BorderLayout.CENTER);

	} 

	/**
	 *  Creates the title panel
	 */
	public void createTitlePanel() {

		titlePanel.setLayout(new FlowLayout());

		// create GUI header
		guiTitle.setText(" Customer Queue ");    

		// set font large and bold 
		guiTitle.setFont(new Font (Font.SANS_SERIF, Font.BOLD,14));

		// add light border
		guiTitle.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));

		guiTitle.setOpaque(true);

		// set background colour to white
		guiTitle.setBackground(Color.WHITE);

		// add it at the top of the title panel
		titlePanel.add(guiTitle); 
	}



	/**
	 * updates the data in the GUI display
	 * by fetching it from the model
	 * @param info   information about the data change
	 */
	public void update(String info) {

		// if a customer has been added to custQueue
		if(info.equalsIgnoreCase("add")) {

			// get the most recently added customer
			Customer custQueue = model.getLast();
			// add customer details in a new row (end of table)
			custsTabModel.addRow(new Object[]{custQueue.getCustID(), custQueue.getFirstName() + " " + custQueue.getLastName(), custQueue.getParcelID()});

		}

		// if a customer has been removed from custQueue
		else if(info.equalsIgnoreCase("rem")) {

			// remove the customer at the head of the queue (1st row in table)
			custsTabModel.removeRow(0);
		}

	}

}